﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ChiaraImages
{
  public interface IInvoker
  {
    IAsyncResult BeginInvoke(Delegate method);
    IAsyncResult BeginInvoke(Delegate method, params object[] args);
  }

  public partial class FormMain : Form, IInvoker
  {
    ImarisProcess m_imaris;
    FormWaitAndShowMessage m_formShowInfo = new FormWaitAndShowMessage();
    public FormMain()
    {
      InitializeComponent();
      m_imaris = new ImarisProcess(this);
      m_imaris.ShowInformation = ShowMessage;
    }

    private void convertToolStripMenuItem_Click(object sender, EventArgs e)
    {
      if (this.m_openFileDialogSelectFilesToConvert.ShowDialog() == DialogResult.OK) 
      {
        CleanUp();
        fileToolStripMenuItem.Enabled = false;
        m_imaris.InitializationImarisAsyn(convertToolStripMenuItemDoneStep);
      }
    }
    private void convertToolStripMenuItemDoneStep(bool result, int step)
    {
      if (!result)
      {
        MessageBox.Show(this, m_imaris.GetError(), "Error", MessageBoxButtons.OK);
        m_imaris.QuitImaris();
        ShowMessage("Error:" + m_imaris.GetError());
        fileToolStripMenuItem.Enabled = true;
      }
      else
      {
        if (step == 1) m_imaris.ConvertAllFilesAsyn(this.m_openFileDialogSelectFilesToConvert.FileNames, true, convertToolStripMenuItemDoneStep, step + 1);
        if (step == 2) m_imaris.QuitImarisAsyn(convertToolStripMenuItemDoneStep, step + 1);
        if (step > 2)
        {
          ShowMessage("Done!");
          fileToolStripMenuItem.Enabled = true;
        }
      }
    }
    public void ShowMessage(string info)
    {
      textBoxMessage.AppendText(info + "\r\n");
    }
    public void CleanUp()
    {
      textBoxMessage.Text = "";
    }
  }
}
